package javaCode;

import java.util.*;

/**
 * Created by lxy_920912 on 02/05/2017.
 */
public class LeetCode496 {
    public int[] nextGreaterElement(int[] findNums, int[] nums) {
        int[] result = new int[findNums.length];
        Stack<Integer> stack = new Stack<>();
        Map<Integer,Integer> map = new HashMap<>();

        for(int i = nums.length-1;i >= 0;i--){
            while (!stack.isEmpty() && stack.peek() <= nums[i]){
                stack.pop();
            }
            if(stack.isEmpty()){
                map.put(nums[i],-1);
            }else{
                map.put(nums[i],stack.peek());
            }
            stack.push(nums[i]);
        }
        for(int i = 0;i < findNums.length;i++){
            result[i] = map.get(findNums[i]);
        }
        return result;
    }
}
